<template>
    <view>
        <view v-if="(detail || null) != null" class="bigpage">
            <!--宣传语-->
            <view class="kan_box">
                <view class="kan_body">
                    <view class="user-info">
                        <image class="img1" :src="detail.user.avatar" mode="aspectFit" />
                        <view class="user-name">{{ detail.user.user_name_view }}</view>
                    </view>
                    <block v-if="(detail.cut_info || null) != null">
                        <view class="p1" v-if="detail.cut_info.share_ftitle != ''">
                            <text class="tt11">“</text>
                            {{ detail.cut_info.share_ftitle }}
                            <text class="tt22">”</text>
                        </view>
                        <view class="p1" v-else>
                            <text class="tt11">“</text>
                            {{ plugins_config.share_ftitle }}
                            <text class="tt22">”</text>
                        </view>
                    </block>

                    <!-- 商品列表 -->
                    <view class="goods bg-white">
                        <block v-if="(detail.cut_info || null) != null">
                            <view class="goods-item oh">
                                <navigator :url="'/pages/fcc/cutprice/detail/detail?cut_id=' + detail.goods_id" hover-class="none">
                                    <image class="goods-image fl" :src="detail.images" mode="aspectFill" />
                                    <view class="goods-base">
                                        <view class="goods-title multi-text">{{ detail.title }}</view>
                                        <block v-if="detail.spec != null">
                                            <view class="goods-attribute cr-888" v-for="(spec, index) in detail.spec" :key="index">{{ spec.type }}:{{ spec.value }}</view>
                                        </block>
                                    </view>
                                </navigator>
                            </view>
                        </block>
                        <block v-else>
                            <view class="goods-item oh tc">{{$t('fengcheche.cutprice.cart_detail_success_cut_text')}}</view>
                        </block>
                        <view class="order-describe">
                            <block v-if="(detail.cut_info || null) != null">
                                <view class="jindutext">
                                    <text class="fr">{{$t('fengcheche.cutprice.cart_detail_price_desc_before')}}{{ currency_symbol }}{{ detail.low_price }}{{$t('fengcheche.cutprice.cart_detail_price_desc_after')}}</text>
                                    <text v-if="detail.original_price > 0" class="">{{$t('fengcheche.cutprice.list_price_old_price')}}{{ currency_symbol }}{{ detail.original_price }}</text>
                                </view>
                                <view class="jindutiao">
                                    <progress :percent="detail.now_jindu" stroke-width="10" border-radius="8" activeColor="#fec330" active />
                                </view>
                                <view class="jindudesc tc">
                                    {{$t('fengcheche.cutprice.cart_detail_cut_desc_before')}}
                                    <text class="t1">{{ currency_symbol }}{{ detail.already_kan }}</text>{{$t('fengcheche.cutprice.cart_detail_cut_desc_after')}}
                                    ，{{$t('fengcheche.cutprice.cart_detail_need_desc_before')}}
                                    <text class="t1">{{ currency_symbol }}{{ detail.now_need_kan }}</text>{{$t('fengcheche.cutprice.cart_detail_need_desc_after')}}
                                    <!-- 时间 -->
                                    <text v-if="detail.status == 0 && detail.time.status == 1" class="timebox">
                                        <text>，{{$t('fengcheche.cutprice.cart_detail_over_desc_before')}}</text>
                                        <text class="shi t1">{{ detail.time.hours }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_1')}}</text>
                                        <text class="fen t1">{{ detail.time.minutes }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_2')}}</text>
                                        <text class="miao t1">{{ detail.time.seconds }}</text>
                                        <text class="dian t1">{{$t('fengcheche.cutprice.cart_detail_time_text_3')}}</text>
                                        <text>{{$t('fengcheche.cutprice.cart_detail_over_desc_after')}}</text>
                                    </text>
                                </view>
                            </block>
                            <view :class="'jindubtn '+$t('fengcheche.cutprice.app_lang')">
                                <block v-if="detail.status > 0">
                                    <view class="jindubtn_cc jd01"><view data-value="/pages/fcc/cutprice/index/index" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.cart_detail_want_it_btn')}}</view></view>
                                    <view class="jindubtn_cc jd02"><view>{{$t('fengcheche.cutprice.cart_detail_fail_text')}}</view></view>
									<view class="cl"></view>
                                </block>
                                <block v-else>
                                    <!--两种情况，一种是对方未砍成，一种是已砍成-->
                                    <block v-if="detail.price == detail.low_price">
                                        <view class="jindubtn_cc jd01"><view :data-id="detail.id">{{$t('fengcheche.cutprice.cart_detail_have_ok_btn')}}</view></view>
                                    </block>
                                    <block v-else>
                                        <view class="jindubtn_cc jd01"><view :data-id="detail.id" @tap="cut_submit_event">{{$t('fengcheche.cutprice.cart_detail_help_her_btn')}}</view></view>
                                    </block>
                                    <view class="jindubtn_cc jd02">
                                        <view :data-value="'/pages/fcc/cutprice/detail/detail?cut_id=' + detail.goods_id" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.cart_detail_want_it_btn')}}</view>
                                    </view>
									<view class="cl"></view>
									<view class="gohome"><view data-value="/pages/index2/index2" data-type="1" @tap="navigation_event">>> {{$t('fengcheche.cutprice.app_shop_index_page_btn_text')}} <<</view></view>
                                </block>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <view class="spacing-nav-title">
                <text class="line"></text>
                <text class="text-wrapper">{{$t('fengcheche.cutprice.detail_base_title_guize_text')}}</text>
            </view>
            <view class="wanfa spacing">
                <view class="wanfacc weui-flex bg-white">
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>1</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_1')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>2</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_2')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>3</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_3')}}
                        </view>
                    </view>
                    <view class="weui-flex__item">
                        <view class="placeholder">
                            <text>4</text>
                            {{$t('fengcheche.cutprice.detail_base_title_guize_4')}}
                        </view>
                    </view>
                </view>
            </view>

            <view class="spacing-nav-title">
                <text class="line"></text>
                <text class="text-wrapper">{{$t('fengcheche.cutprice.cart_detail_who_help_me_title')}}</text>
            </view>
            <view class="panel-item spacing-mt teamlist">
                <view class="panel-content bg-white">
                    <block v-if="detail.teamusers || (null != null && detail.teamusers.length > 0)">
                        <view class="tuan_li" v-for="(item, index) in detail.teamusers" :key="index">
                            <image :src="item.user.avatar" mode="aspectFill" />

                            <view class="whois">
                                <text class="t1">{{ item.user.user_name_view }}</text>
                                <text class="t2" :decode="true">{{ item.add_time }}</text>
                            </view>

                            <view class="liright">
                                <view class="tinfo">{{$t('fengcheche.cutprice.cart_detail_cut_howmuch_before')}}{{ currency_symbol }}{{ item.price }}{{$t('fengcheche.cutprice.cart_detail_cut_howmuch_after')}}</view>
                            </view>
                        </view>
                    </block>
                    <block v-else>
                        <view v-if="detail.teamusers.length == 0" class="tuan_li tc none_jilu">{{$t('fengcheche.cutprice.cutprice_no_record')}}</view>
                    </block>
                </view>
            </view>
			
			<!-- 弹层 -->
			<popup :show="popup_status" @onclose="popup_close_event">
			    <view class="share-popup">
			        <view class="close fr oh">
			            <view class="fr" @tap.stop.prevent="popup_close_event">
			                <icon type="clear" size="30" />
			            </view>
			        </view>
			        <view class="share-popup-head">
			            <image :src="cutprice_static_url+'mini/bg.png'" mode="aspectFit" />
			            <view class="ttss">{{$t('fengcheche.cutprice.cart_detail_kanjia_success')}}</view>
			        </view>
			        <view class="share-popup-content bangpopup">
			            <view class="ttss">
			                {{$t('fengcheche.cutprice.cart_detail_how_much_you_do_before')}}
			                <text>{{ currency_symbol }}{{ kan_price }}</text>
							{{$t('fengcheche.cutprice.cart_detail_how_much_you_do_after')}}
			            </view>
			            <view class="jindubtn_cc jd01 jindu_big">
			                <view :data-value="'/pages/fcc/cutprice/detail/detail?cut_id=' + detail.goods_id" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.cart_detail_help_pop_want_it_btn')}}</view>
			            </view>
			            <view class="jindubtn_cc jd02 jindu_big"><view data-value="/pages/fcc/cutprice/index/index" data-type="1" @tap="navigation_event">{{$t('fengcheche.cutprice.app_title')}}</view></view>
			        </view>
			    </view>
			</popup>
        </view>

        <view v-else>
            <!--<view class="kan_box">
                <view class="kan_body">
                    <view class="goods bg-white">
                        <view class="goods-item oh tc">该砍价已失效</view>
                    </view>
                </view>
            </view>-->

            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </view>
		
		<component-popup :propShow="is_show_fcc_tips_popup_box" propPosition="center" @onclose="app_download_popup_close">
			<view v-if="is_show_fcc_tips_popup_box" class="fcc_tips_popup_box">
				<view class="mask flex-center">
					<view class="content botton-radius">
						<view class="content-top">
							<text class="content-top-text">{{$t('fengcheche.cutprice.wenxin_tips_title')}}</text>
						</view>
						<view class="content-body">
							<view class="title">
								<text>{{$t('fengcheche.cutprice.down_app_tips')}}</text>
							</view>
							<view class="footer flex-center">
								<button class="content-button" style="border: none;color: #fff;" plain @click="app_download_aciton" data-value="andriod">
									{{$t('fengcheche.downappbtn')}}
								</button>
							</view>
						</view>
				
						<image class="close-img" src="../../../../../uni_modules/uni-upgrade-center-app/images/app_update_close.png"
							@click.stop="app_download_popup_close"></image>
					</view>
				</view>
			</view>
		</component-popup>
    </view>
</template>

<script>
const app = getApp();
import popup from '@/pages/fcc/cutprice/popupzhuanpan/index';
import componentPopup from '@/pages/fcc/cutprice/popup/popup';
import componentNoData from "@/components/no-data/no-data";
import { cutprice_base_info } from '@/pages/fcc/cutprice/public/common.js';
import {system_base_info, clear_user_cach} from "@/common/js/common/common";

var cutprice_static_url = app.globalData.get_static_url('cutprice', true);
var common_static_url = app.globalData.get_static_url('common');
export default {
    components: {
        popup,
		componentNoData,
		componentPopup
    },
    data() {
        return {
            currency_symbol: app.globalData.currency_symbol(),
			common_static_url: common_static_url,
			cutprice_static_url: cutprice_static_url,
            params: null,
            data_list_loding_status: 1,
            data_list_loding_msg: '',
            data_bottom_line_status: false,
            detail: null,
            site_fictitious: null,
            plugins_config: null,
            popup_status: false,
            kan_price: 0,
            avatar: '',
            user_name_view: '',
            share_ftitle: '',
            status: 0,
            hours: '',
            minutes: '',
            seconds: '',
            length: 0,
			have_kan:false,
			// 自定义分享信息
			share_info: {},
			// 多语言
			lang_info:{},
			diytheme: null,
			diysysbase: null,
			is_show_fcc_tips_popup_box:false,
			help_com_time:0,
        };
    },
	onShow() {
		// 进入该页面，如果是帮砍者进来，如果此人已登录，则直接取消剪贴板
		var user = app.globalData.get_user_cache_info();
		if(user != null) {
			app.globalData.clear_linker_cut_cache();
		}else{
			// 未登录，则记录来该页面次数，如果其正常点击砍价去登录，返回是可以顺利清除剪贴板的，如果其不愿意帮砍，则此时记录就有用作，帮助其离开该页面
			if(this.help_com_time > 1)
			{
				app.globalData.clear_linker_cut_cache();
			}
			let new_help_com_time = this.help_com_time+1;
			this.setData({
			    help_com_time: new_help_com_time
			});
		}
	},
    onLoad(params) {
        //params['id'] = 4;
        this.setData({
            params: params
        });
		this.init();
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.init();
    },
    methods: {
		// 加载之前
		init() {
			// 先获取语言基础信息
			cutprice_base_info()
			.then(data=>{
				this.init_before(data)
				.then(()=>{
					system_base_info()
					.then(data=>{
						this.sys_init(data);
						this.init_real();
					});
				});
			});
		},
		// 设置系统基础参数
		sys_init(data){
			this.setData({
				diytheme: data.diytheme,
				diysysbase: data.base,
			});
		},
		// 设置本页参数
		init_before(data){
			var self = this;
			return new Promise((resolve, reject) => {
				self.setData({
					lang_info: data.cutprice_lang_info,
					currency_symbol: app.globalData.get_config('currency_symbol'),
				});
				uni.setNavigationBarTitle({
				    title: self.$t('fengcheche.cutprice.cart_detail_page_title')
				});
				resolve();
			});
		},
        init_real() {
            var self = this;
            uni.showLoading({
                title: this.$t('fengcheche.cutprice.loading_tips')
            });
            this.setData({
                data_list_loding_status: 1
            }); 
			
			// 首次打开，无须获取用户授权，因为打开着是要 帮着砍价的，很大可能是第一次进来，所以调用新的方法
            uni.request({
                url: app.globalData.get_request_url('sharedetail', 'index', 'cutprice'),
                method: 'POST',
                data: {
                    id: this.params.id
                },
                dataType: 'json',
                success: (res) => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    console.log(res);

                    if (res.data.code == 0) {
                        var data = res.data.data; 
						
						// 如果当前分享页就是当前用户的，则给跳转到其正常的砍价详情去，因为此页面是非本人查看的
                        var user_info = app.globalData.get_user_cache_info();
                        var user_id = (user_info || null) != null ? user_info.id : 0;
						
						if ((data.data || null != null) && parseInt(data.data.user_id) == parseInt(user_id)) {
							// 设置一个自己返回的标识
							uni.setStorageSync('linker_cut_is_self_url_flag_cache_key', 1);
							app.globalData.url_open('/pages/fcc/cutprice/cart/detail/detail?id=' + data.data.cart_id);
						    return false;
						}
						
                        self.setData({
                            detail: data.data,
                            plugins_config: data.plugins_config,
                            site_fictitious: data.site_fictitious || null,
                            data_list_loding_status: 3,
                            data_bottom_line_status: true,
                            data_list_loding_msg: '',
							// 基础自定义分享
							share_info: {
							    title: (data.data.cut_info.share_title != '') ? data.data.cut_info.share_title : data.plugins_config.share_title,
							    kds: data.data.title,
							    desc: data.data.title,
							    path: '/pages/fcc/cutprice/cart/share/share',
							    query: 'id=' + data.data.id,
							    img: data.data.images,
							    video: data.data.video
							}
                        });
                    } else {
                        self.setData({
                            data_list_loding_status: 2,
                            data_bottom_line_status: false,
                            data_list_loding_msg: res.data.msg
                        });

                        if (app.globalData.is_login_check(res.data, self, 'init')) {
                            app.globalData.showToast(res.data.msg);
                        }
                    }
					
					// 分享菜单处理
					app.globalData.page_share_handle(this.share_info);
                },
                fail: () => {
                    uni.hideLoading();
                    uni.stopPullDownRefresh();
                    self.setData({
                        data_list_loding_status: 2,
                        data_bottom_line_status: false,
                        data_list_loding_msg: this.$t('fengcheche.cutprice.request_data_error_tips')
                    });
                    app.globalData.showToast(this.$t('fengcheche.cutprice.request_data_error_tips'));
                }
            });
        },
		
		// 下载APP提示
		app_download_aciton(){
			// 获取用户设备类型
			const userAgent = navigator.userAgent.toLowerCase();
			let downloadUrl;
			console.log(userAgent);
			var url = this.diysysbase.app_download_url || null;
			if((url || null) == null)
			{
				app.globalData.showToast(this.$t('goods-detail.goods-detail.tq1976'));
			}
			// 判断是iOS还是Android设备
			if (/iphone|ipad|ipod/.test(userAgent)) {
				// iOS设备，跳转到App Store
				downloadUrl = this.diysysbase.app_download_url_ios || url;//'https://itunes.apple.com/us/app/your-app-name/idYOUR_APP_ID?mt=8'; // 替换YOUR_APP_ID为你的App Store应用ID
		
			}else if (/android/.test(userAgent)) {
				// Android设备，跳转到Google Play或其他应用商店
				// 这里以Google Play为例，但你可能需要根据用户的具体地区或设备类型调整链接
				downloadUrl = url; // 替换com.yourcompany.yourapp为你的Android应用包名
			} else {
				// 其他设备类型，可以处理或忽略
				downloadUrl = this.diysysbase.app_download_url_hong || url;
			}
			// 跳转到下载链接
			console.log(downloadUrl);
			//window.location.href = downloadUrl;
			// 浏览器跳转
			window.open(downloadUrl, '_blank'); 
		},
		
		app_download_popup_close()
		{
			this.setData({
			    is_show_fcc_tips_popup_box: false,
			});
		},

        // 帮砍一刀
        cut_submit_event(e) {
			if (!app.globalData.is_single_page_check()) {
			    return false;
			}
			var self = this;
			// 先看是否登录，未登录，则记录该动作
			var user = app.globalData.get_user_cache_info();
			if(user == null) {
				// H5中点击帮砍，会先记剪贴，APP中也是，防止当前页面他并未登录
				uni.setClipboardData({
					data: 'linker-cutprice-help-'+self.detail.id,
					showToast: false,
					success: function () {}
				});
				// 同时存入缓存
				uni.setStorageSync('linker_cut_data_cache_key', 'linker-cutprice-help-'+self.detail.id);
			}
			
			// 帮砍需要登录
			var user = app.globalData.get_user_info(this, 'cut_submit_event');
			if (user != false) {
				// 如果当前分享页就是当前用户的，则给跳转到其正常的砍价详情去，因为此页面是非本人查看的
				var user_info = app.globalData.get_user_cache_info();
				var user_id = (user_info || null) != null ? user_info.id : 0;
				
				if ((self.detail || null != null) && parseInt(self.detail.user_id) === parseInt(user_id)) {
					// 设置一个自己返回的标识
					uni.setStorageSync('linker_cut_is_self_url_flag_cache_key', 1);
					app.globalData.url_open('/pages/fcc/cutprice/cart/detail/detail?id=' + self.detail.cart_id);
					return false;
				}
				
				// 帮砍
				uni.request({
					url: app.globalData.get_request_url('bang', 'cart', 'cutprice'),
					method: 'POST',
					data: {
						team_id: self.detail.id
					},
					dataType: 'json',
					success: (res) => {
						uni.hideLoading();
						uni.stopPullDownRefresh();
						//console.log(res);
						
						app.globalData.clear_linker_cut_cache();
						
						if (res.data.code == 0) {
							self.setData({
								popup_status: true,
								kan_price: res.data.data.money,
								have_kan:true,
							});
						} else {
							app.globalData.showToast(res.data.msg);
						}
					},
					fail: () => {
						uni.hideLoading();
						uni.stopPullDownRefresh();
						app.globalData.showToast(this.$t('fengcheche.cutprice.request_data_error_tips'));
					}
				});
			}
        },

        // 购买弹层关闭
        popup_close_event(e) {
            this.setData({
                popup_status: false
            });
			// 如果砍价成功时点击关闭，则重新加载页面
			if(this.have_kan){
				this.init();
			}
        },

        // 导航事件
        navigation_event(e) {
            app.globalData.operation_event(e);
        }
    }
};
</script>
<style>
@import './share.css';
</style>
